<template>
  <table-layout :haveSider="false" :isStandTable="false">
    <template #headCustom>
      <a-button type="primary" @click="onBack">返回</a-button>
      <a-button type="primary" style="marginLeft:20px" @click="onSure">确认</a-button>
      <!-- <a-button type="primary" style="marginLeft:20px" @click="onReset">重置</a-button> -->
    </template>
    <template #form>
      <div class="parbox">
        <div class="box1">
          <basic-form
            v-if="isExit"
            style="marginTop:50px"
            ref="form"
            :isBtn="false"
            :formInfos="s_FormInfo"
            :formDatas="s_formDatas"
            :rules="s_rules"
            @onSubmit="onSubmit"
            @onChangeType="onChangeType"
            @inputOnSearch="inputOnSearch"
            @orgsearch="orgsearch"
          ></basic-form>
        </div>
        <div class="box2">
          <div class="box2">
            <a-button type="primary" @click="onSure" >确认</a-button>
            <a-button type="primary" @click="onBack" style="marginLeft:20px" >取消</a-button>
          </div>
        </div>
      </div>
      
    </template>
  </table-layout>
</template>

<script>
import searchLayout from '@/views/app/biz/common/searchLayout'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'
import tableLayout from '@/views/app/biz/common/index'
import basicForm from '../common/basicForm'
import storage from 'store'
import { getOrgInfoByPid } from '@/views/sys/org/org/org'
import { getInfoByPid, saveInfo } from './customer'
import { s_FormInfo, s_formDatas, s_rules } from './customerConfig'

export default {
  components: {
    searchLayout,
    batchOperatBtn,
    tableLayout,
    basicForm
  },
  data() {
    return {
      isExit:true,
      s_FormInfo: s_FormInfo,
      s_formDatas: s_formDatas,
      s_rules: s_rules
    }
  },
  computed: {},
  activated(){
    console.log('activated',this.$route );
    this.backPathName = this.$route.params.pathName ? this.$route.params.pathName : 'customerInfo'
    //如果传了type值表示为重新点击进去的，需要初始化数据，如果未传值表示缓存在页签中，需路由配置keepAlive
    if (this.$route.params.type=='edit') {
        this.editModal(this.$route.params.pid)
    }
    if(this.$route.params.type=='add'){
        this.addModal()
    }
  },
  methods: {
    onBack() {
      
      this.$router.push({
        name: this.backPathName,
        params: {
          isJump: false,
          pathName: this.$route.name,
          iscloseMul:true,
        }
      })
    },
    initData(){
      console.log('重置数据');
      Object.assign(this.$data, this.$options.data.call(this))
      this.isExit=false
      this.$nextTick(()=>{
        this.isExit=true
      })
      
    },
    // 新增按钮 点击了新增
    addModal() {
      this.initData()
      for (let item of this.s_FormInfo) {
        if (item.name === 'keHushxxdm') {
          item.isDisabled = false
        } else {
          item.isDisabled = true
        }
      }
      
    },
    // 编辑按钮
    editModal(val) {
      this.initData()
      this.getSBypid(val)
      for (let item of this.s_FormInfo) {   
        if (item.name === 'keHushxxdm'
           ||item.name === 'keHudm'
           ||item.name === 'keHumc'
           ||item.name === 'chengShi') {
          item.isDisabled = true
        } else {
          item.isDisabled = false
        }
      }
    },
    onReset(){
      this.$refs.form.resetForm()
    },
    onSure() {
      this.$refs.form.submitForm()
    },
    // 提交按钮
    onSubmit(val) {
      console.log(val);
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.onBack()
      } else {
        this.$message.error({ content: '提交失败，原因：'+res.message, duration: 2 })
        this.addModal()
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      console.log('formName', name)
      console.log('val', val)
      switch (name) {
        case 'keHuOrgPid':
          this.getOrgInfo(val)
          break
      }
    },
    // 查询单位信息
    async getOrgInfo(val) {
      let res = await getOrgInfoByPid(val, {})
      if (res && res.code && res.code === 'C1' && res.data) {
        this.s_formDatas.keHumc = res.data.orgFullName
      }
    },
    // input搜索
    inputOnSearch(val, name) {
      console.log(val)
      console.log(name)
      switch (name) {
        case 'keHuOrgPid':
          this.getOrgInfo(val)
          break
      }
    },
    // 单位搜索
    orgsearch(name, val) {
      console.log('val', val)
      console.log('name', name)
      this.s_formDatas.keHudm = val.orgIdno
      this.s_formDatas.keHushxxdm = val.orgIdno
      this.s_formDatas.keHuOrgPid = val.org.pid
      this.s_formDatas.keHumc = val.org.orgName
      this.s_formDatas.chengShi = val.org.city
      this.s_formDatas.fuZerxm = val.org.orgBossName
      for (let item of this.s_FormInfo) {
        if (item.name === 'keHushxxdm'
           ||item.name === 'keHudm'
           ||item.name === 'keHumc'
           ||item.name === 'chengShi')  {
          item.isDisabled = true
        } else {
          item.isDisabled = false
        }
      }
    },
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    }
  }
}
</script>

<style>
.viewTitle {
  line-height: 30px;
  font-size: 16px;
  font-weight: bolder;
  color: #555;
  border-bottom: 1px solid #ccc;
  margin: 10px 0;
}
.upAndDown {
  text-align: center;
}
.card {
  width: 100%;
  padding: 0 7px;
}
.dis {
  height: 15px;
  background-color: #f0f2f5;
}
</style>
